package com.bootssm.controller;

import com.bootssm.pojo.*;
import com.bootssm.pojo.Record;
import com.bootssm.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;

    @PostMapping("/findAll")
    public PageView<Record> findAll(@RequestBody RecordVO recordVO) {
        List<Record> recordList = recordService.findAll(recordVO);
        Integer total = recordService.getTotal(recordVO);
        PageView<Record> pageView = new PageView<>();
        pageView.setDataList(recordList);
        pageView.setCount(total);
        return pageView;
    }

    @DeleteMapping("/delete/{recordId}")
    public Result deleteId(@PathVariable("recordId") String recordId){
        try {
            recordService.delete(recordId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","删除失败");
        }
        //保存数据库
        return new Result("200","删除成功");
    }

    @PostMapping("/save")
    public Result save(@RequestBody Borrow borrow){
        try {
            recordService.insert(borrow);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","添加失败");
        }
        //保存数据库
        return new Result("200","添加成功");
    }

    @GetMapping("/findById/{recordId}")
    public Record findById(@PathVariable("recordId") String recordId){
        return recordService.findById(recordId);
    }
}
